package com.yihu.wlyy.service.app.health;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;
import org.springside.modules.persistence.SearchFilter.Operator;

import com.yihu.wlyy.entity.health_guide.HealthNews;
import com.yihu.wlyy.repository.HealthNewsDao;
import com.yihu.wlyy.service.BaseService;

/**
 * 健康资讯业务处理类
 * @author George
 *
 */
@Component
@Transactional(rollbackFor = Exception.class)
public class HealthNewsService extends BaseService {

	@Autowired
	private HealthNewsDao healthNewsDao;

	/**
	 * 获取文章列表
	 * @param page 页码
	 * @param pagesize 分页大小
	 * @return 列表
	 */
	public Page<HealthNews> findAll(long id, int pagesize) {
		if (pagesize <= 0) {
			pagesize = 10;
		}
		// 排序
		Sort sort = new Sort(Direction.DESC, "id");
		// 分页信息
		PageRequest pageRequest = new PageRequest(0, pagesize, sort);
		// 设置查询条件
		Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
		if (id > 0) {
			filters.put("id", new SearchFilter("id", Operator.LT, id));
		}
		// 未作废
		filters.put("del", new SearchFilter("del", Operator.EQ, "1"));
		Specification<HealthNews> spec = DynamicSpecifications.bySearchFilter(filters.values(), HealthNews.class);
		return healthNewsDao.findAll(spec, pageRequest);
	}
}
